package com.jh3502.vivo.function.dialog;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseActivity;
import com.jh3502.vivo.utils.Constant;
import com.jh3502.vivo.utils.UploadHelper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;

/**
 * @author harisucici
 * @version 2018/12/14 08:32
 * name
 * todo
 * @email harisucici@gmail.com
 */
public class PhotoChooseDialog extends BaseActivity<PhotoChoosePresenter.PhotoChooseView, PhotoChoosePresenter> implements PhotoChoosePresenter.PhotoChooseView {


    private String path;
    private List<File> photos = new ArrayList<>(2);

    @Override
    public PhotoChoosePresenter initPresenter() {
        return new PhotoChoosePresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.dialog_choose_photo;
    }

    @Override
    protected void initView() {
        mPresenter.setPhotoChooseView(this);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case Constant.REQUEST_CODE_IMAGE:
                    path = UploadHelper.getInstance().getImageAbsolutePath(this, data.getData());
                    photos.add(new File(path));
                    mPresenter.toUpload(photos);
                    break;
                case Constant.REQUEST_CODE_CAMERA:
                    showLoadingDialog();
                    new Handler().postDelayed(() -> {
                        photos.add(mCamareFile);
                        mPresenter.toUpload(photos);
                    }, 5000);
                    break;
                default:
            }
        }
    }

    @OnClick(R.id.dialog_choose_pic)
    public void onDialogChoosePicClicked() {
        chooseLocImage();
    }

    @OnClick(R.id.dialog_choose_camera)
    public void onDialogChooseCameraClicked() {
        chooseLocCamera("");
    }

    @Override
    public void uploadSuccess(String s) {
        Intent intent = new Intent();
        intent.putExtra("path", s);
        intent.putExtra("positionMain", getIntent().getIntExtra("positionMain", -1));
        intent.putExtra("typeItem", getIntent().getIntExtra("typeItem", -1));
        intent.putExtra("positionItem", getIntent().getIntExtra("positionItem", -1));
        setResult(Activity.RESULT_OK, intent);
        finish();

    }
}
